﻿using Microsoft.AspNetCore.Components;
using Production.CommonLib.Models;
using Production.RazorLib.Services;
using Radzen;

namespace Production.RazorLib.Pages;

public partial class ProductionOrderEdit
{
    private ProductionOrderEditRequestDto _editRequestDto = new();
    private IList<StandardBomInfoResponseDto> _standardBoms = new List<StandardBomInfoResponseDto>();
    private int _count = 0;

    [Inject] public IProductionOrderService ProductionOrderService { get; set; }

    [Inject] public IStandardBomService StandardBomService { get; set; }

    [Inject] public DialogService DialogService { get; set; }

    [Parameter] public int Key { get; set; }

    void CancelButtonClick()
    {
        DialogService.Close();
    }

    async Task OnSubmit()
    {
        if (Key > 0)
        {
            var result = await ProductionOrderService.UpdateProductionOrder(Key, _editRequestDto);

            if (result.Succeeded)
            {
                DialogService.Close();
            }
        }
        else
        {
            var result = await ProductionOrderService.CreateProductionOrder(_editRequestDto);

            if (result != null && result.Succeeded)
            {
                DialogService.Close();
            }
        }
    }

    protected override async Task OnParametersSetAsync()
    {
        _editRequestDto = new ProductionOrderEditRequestDto();
        if (Key > 0)
        {
            var result = await ProductionOrderService.GetProductionOrder(Key);
            if (result != null && result.Succeeded && result.Data != null)
            {
                _editRequestDto.Code = result.Data.Code;
                _editRequestDto.Name = result.Data.Name;
                _editRequestDto.OrderSource = result.Data.OrderSource;
                _editRequestDto.OrderStatus = result.Data.OrderStatus;
                _editRequestDto.StandardBom.Id = result.Data.StandardBomId;
                _editRequestDto.StandardBom.Code = result.Data.StandardBomCode;
                _editRequestDto.StandardBom.Name = result.Data.StandardBomName;
                _editRequestDto.Quantity = result.Data.Quantity;
                _editRequestDto.ExpectedTime = result.Data.ExpectedTime;
                _editRequestDto.EndTime = result.Data.EndTime;
                _editRequestDto.PlanStartTime = result.Data.PlanStartTime;
                _editRequestDto.PlanEndTime = result.Data.PlanEndTime;
                _editRequestDto.Description = result.Data.Description;
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var result = await StandardBomService.QueryStandardBomsByPage(new StandardBomSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            _standardBoms = result.Data!;
            _count = result.TotalCount;
        }
    }

    async Task LoadData(LoadDataArgs args)
    {
        var result = await StandardBomService.QueryStandardBomsByPage(new StandardBomSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            _standardBoms = result.Data!;
            _count = result.TotalCount;
        }

        await InvokeAsync(StateHasChanged);
    }
}